package com.zhongya.example.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RadialGradient
import android.graphics.Shader
import android.util.AttributeSet
import android.view.View

class RadialGradientView : View {

    val mPaint = Paint()
    var mRadialGradient : RadialGradient? = null
    var mRadius = 0f

    constructor(context : Context) : super(context)

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs){
        setLayerType(LAYER_TYPE_SOFTWARE,null)

    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if(mRadialGradient == null){
            mRadius = width/2f
            mRadialGradient =
                /*RadialGradient(
                    width/2f,
                    height/2f,
                    mRadius,
                    0xffff0000.toInt(),
                    0xff00ff00.toInt(),
                    Shader.TileMode.REPEAT
                )*/
            RadialGradient(
                width/2f,
                height/2f,
                mRadius,
                intArrayOf(0xffff0000.toInt(),0xff00ff00.toInt(),0xff0000fff.toInt(),0xffffff00.toInt()),
                floatArrayOf(0f,0.2f,0.5f,1f),
                Shader.TileMode.REPEAT
            )
            mPaint.shader = mRadialGradient
        }
        canvas?.drawCircle(width/2f,height/2f,mRadius,mPaint)
//        canvas?.drawRect(0f,0f,width.toFloat(),height.toFloat(),mPaint)
    }
}